var roleCarrierio = {

    /** @param {Creep} creep **/
    run: function(creep) {
        // creep.say(creep.name);
        //creep.say(creep.memory.building);
        if(creep.store[RESOURCE_ENERGY] == 0){
            creep.memory.building = true;
        }
        else{
            creep.memory.building = false;
        }

        if(creep.memory.building == true){
            var targetsmubei = creep.room.find(FIND_TOMBSTONES, {
                filter: (str) => {
                    return str.store.getUsedCapacity(RESOURCE_ENERGY)<100;//大于100的墓碑
                }
            });
            var targetsres = creep.room.find(FIND_DROPPED_RESOURCES);
            var targetsgo = creep.room.find(FIND_STRUCTURES, {
                filter: (structure) => {
                    return structure.structureType == STRUCTURE_STORAGE && structure.store.getUsedCapacity() > 0;
                }
            });
            if(0) {
                creep.say('有墓碑');
                if(creep.withdraw(targetsmubei[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(targetsmubei[0],{visualizePathStyle: {stroke: '#00ff00'}});
                }
            }
            else if(0){
                creep.say('有掉落');
                if(creep.pickup(targetsres[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                creep.moveTo(targetsres[0],{visualizePathStyle: {stroke: '#00ff00'}});
                }
            }
            else if(targetsgo.length > 0) {
                if(creep.withdraw(targetsgo[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(targetsgo[0]);
                    //creep.say('运来');
                }
            }   

        }
        else{
            var targets = creep.room.find(FIND_STRUCTURES, {
                filter: (structure) => {
                    return  ((structure.structureType == STRUCTURE_TOWER) && 
                            structure.store.getFreeCapacity(RESOURCE_ENERGY) > 500);
                }
            });
            if(targets.length > 0) {
                if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(targets[0]);
                    //creep.say('运去');
                }
            }
            else{
                var targetscont = creep.room.find(FIND_STRUCTURES, {
                    filter: (structure) => {
                        return ((structure.structureType == STRUCTURE_EXTENSION ||
                            structure.structureType == STRUCTURE_SPAWN) && 
                            structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0) ||
                                (structure.structureType == STRUCTURE_TERMINAL &&
                                structure.store.getUsedCapacity(RESOURCE_ENERGY) < 50000);//diyu50000+
                    }
                });
                if(targetscont.length > 0) {
                    if(creep.transfer(targetscont[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                        creep.moveTo(targetscont[0]);
                        //creep.say('运去');
                    }
                }
            }
        }
	}
};

module.exports = roleCarrierio;